package com.rem.concurrency.juc.my13Single;

import java.lang.reflect.Constructor;

/**
 * 本身也是一个Class类
 *
 * @author Rem
 * @date 2021-03-11
 */

public enum EnumSingle {

    INSTANCE;

    private EnumSingle getInstance() {
        return INSTANCE;
    }
}

class Test2 {
    public static void main(String[] args) throws Exception {
        //EnumSingle instance = EnumSingle.INSTANCE;
        Class<EnumSingle> enumSingleClass = EnumSingle.class;

        //报错 实际的构造是带参的 NoSuchMethodException: com.rem.concurrency.juc.single.EnumSingle.<init>()
        //Constructor<EnumSingle> declaredConstructor = enumSingleClass.getDeclaredConstructor();

        /**
         * java.lang.IllegalArgumentException: Cannot reflectively create enum objects
         * 证明使用枚举 本身通过反射是不能创建实例的
         */
        Constructor<EnumSingle> declaredConstructor = enumSingleClass.getDeclaredConstructor(String.class, int.class);
        declaredConstructor.setAccessible(true);

        EnumSingle enumSingle = declaredConstructor.newInstance();
    }
}
